package org.budo.mongo.jdbc.driver.statement.impl;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.budo.jdbc.driver.AbstractBudoJdbcPreparedStatement;
import org.budo.jdbc.driver.resultset.impl.IteratorResultSet;
import org.budo.mongo.jdbc.driver.executor.MongoStatementExecutor;
import org.budo.mongo.jdbc.driver.statement.AbstractMongoStatement;
import org.budo.mongo.jdbc.driver.statement.StatementDocument;

/**
 * @author lmw
 */
public class MongoAggregateStatement extends AbstractMongoStatement {
    @Override
    public void execute(PreparedStatement preparedStatement) throws SQLException {
        StatementDocument statementDocument = this.statementDocument(preparedStatement);
        List<Map<String, Object>> aggregatePipeline = statementDocument.getAggregatePipeline();

        MongoStatementExecutor statementExecutor = this.statementExecutor(preparedStatement);
        Iterator<?> iterator = statementExecutor.aggregate(this.getCollectionName(), aggregatePipeline);

        IteratorResultSet resultSet = new IteratorResultSet(this.getResultSetMetaData(), iterator);
        ((AbstractBudoJdbcPreparedStatement) preparedStatement).setResultSet(resultSet);
    }
}